package com.insurance.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.dto.BidBillEntityDto;
import com.insurance.common.dto.ProposalCustomerEntityDto;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import com.insurance.entity.ProposalEntity;
import com.insurance.pingan.bid.dto.download.InvoiceRequest;
import com.insurance.pingan.bid.service.PingAnBidService;
import com.insurance.pingan.bid.utils.PingAnBidRequestUtil;
import com.insurance.service.dao.BidBillDao;
import com.insurance.service.entity.BidBillEntity;
import com.insurance.service.service.BidBillService;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("bidBillService")
public class BidBillServiceImpl extends ServiceImpl<BidBillDao, BidBillEntity> implements BidBillService {

    /**
     * 平安Service
     */
    @Autowired
    private PingAnBidService pingAnBidService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BidBillEntity> page = this.page(
                new Query<BidBillEntity>().getPage(params),
                new QueryWrapper<BidBillEntity>()
        );

        return new PageUtils(page);
    }

    //region 通过订单主键查询

    /**
     * 通过订单主键查询
     *
     * @param orderId 订单主键
     * @return
     */
    @Override
    public BidBillEntity queryByOrderId(String orderId){
        QueryWrapper<BidBillEntity> bidBillQueryWrapper = new QueryWrapper<BidBillEntity>();
        bidBillQueryWrapper.eq("order_id", orderId);
        return this.baseMapper.selectOne(bidBillQueryWrapper);
    }
    //endregion

    //region 通过投保单主键查询

    /**
     * 通过投保单主键查询
     *
     * @param proposalId 投保单主键
     * @return
     */
    @Override
    public BidBillEntity queryByProposalId(String proposalId){
        QueryWrapper<BidBillEntity> bidBillQueryWrapper = new QueryWrapper<BidBillEntity>();
        bidBillQueryWrapper.eq("proposal_id", proposalId);
        return this.baseMapper.selectOne(bidBillQueryWrapper);
    }
    //endregion

    /**
     * 保存发票信息并打印
     *
     * @param bidBillEntity 发票实体
     * @param taxPayerNO 发票纳税人编号
     * @param proposalEntity 投保单实体
     * @param proposalCustomerEntityDto 投保人dto实体
     * @return 返回发票信息字节数组
     * @throws Exception
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public byte[] saveAndPrintInvoice(BidBillEntity bidBillEntity, String taxPayerNO,
                   ProposalEntity proposalEntity, ProposalCustomerEntityDto proposalCustomerEntityDto) throws Exception{
        int flag = this.baseMapper.insert(bidBillEntity);
        if(flag != 1){
            throw new Exception("【保存发票信息失败】");
        }
        BidBillEntityDto bidBillEntityDto = new BidBillEntityDto();
        BeanUtils.copyProperties(bidBillEntity, bidBillEntityDto);
        //转换报文对象
        InvoiceRequest invoiceRequest = PingAnBidRequestUtil.createInvoiceRequest(bidBillEntityDto, proposalEntity.getInsuranceNo(), proposalCustomerEntityDto);
        //请求打印发票
        String result = pingAnBidService.printInvoice(JSONObject.fromObject(invoiceRequest).toString());
        //校验获取内容
        byte[] content = PingAnBidRequestUtil.printInvoice(result);
        return content;
    }

}